/**
 * 
 * Author：liaozhanggen
 * Create Date：2017年2月18日
 * Version：v2.0
 */
package com.choosefine.it.cprivilege.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.cprivilege.model.dto.AccountDTO;
import com.choosefine.it.cprivilege.model.dto.AccountSearchDTO;
import com.choosefine.it.cprivilege.model.vo.AccountVO;
import com.choosefine.it.cprivilege.service.AccountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 控制台——帐户管理
 * Author：liaozhanggen
 * Create Date：2017年2月18日
 * Version：v2.0
 */
@Api(value="控制台——帐户管理",description="廖仉根")
@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {

	@Autowired
	private AccountService accountService;
	
	/**
	 * 新增账号
	 * @throws BusinessException 
	 *
	 * @Author：liaozhanggen
	 * @Create Date：2017年2月18日
	 * @param：....
	 * @return：String
	 */
	@ApiOperation(value="新增账号")
	@PostMapping("/addaccount")
	public String addAccount(@RequestBody AccountDTO accountDTO) throws BusinessException{
		//校验数据的有效性
		String realName = accountDTO.getRealName();
		if (StringUtils.isEmpty(realName) || realName.length()>12) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.ACCOUNT_NAME_VALID_LENGH);
		}
		String account = accountDTO.getUserAccount();
		if (StringUtils.isEmpty(account) || account.length()>32) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.ACCOUNT_NUM_VALID_LENGH);
		}
		String pwd = accountDTO.getPassword();
		if (StringUtils.isEmpty(pwd) || pwd.length()< 6 ||pwd.length() > 12) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.ACCOUNT_PASSWORD_VALID_LENGH);
		}
		String operatorAcc = accountDTO.getOperatorAccount();
		if (StringUtils.isEmpty(operatorAcc)) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.OPERATOR_ACCOUNT_VALID_IS_NULL);
			
		}
		//添加账户功能
		int num = accountService.addAccount(accountDTO);
		
		return num > 0 ? responseSuccess() : responseFail();
	}
	
	/**
	 * 编辑账号
	 * @throws BusinessException 
	 *
	 * @Author：liaozhanggen
	 * @Create Date：2017年2月18日
	 * @param：....
	 * @return：String
	 */
	@ApiOperation(value="编辑账号")
	@PostMapping("/editaccount")
	public String modifyAccount(@RequestBody AccountDTO accountDTO) throws BusinessException{
		//校验数据的有效性
		String realName = accountDTO.getRealName();
		if (StringUtils.isEmpty(realName) || realName.length()>12) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.ACCOUNT_NAME_VALID_LENGH);
		}
		String account = accountDTO.getUserAccount();
		if (StringUtils.isEmpty(account) || account.length()>32) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.ACCOUNT_NUM_VALID_LENGH);
		}
		String pwd = accountDTO.getPassword();
		if (StringUtils.isEmpty(pwd) || pwd.length()< 6 ||pwd.length() > 12) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.ACCOUNT_PASSWORD_VALID_LENGH);
		}
		String operatorAcc = accountDTO.getOperatorAccount();
		if (StringUtils.isEmpty(operatorAcc)) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.OPERATOR_ACCOUNT_VALID_IS_NULL);
			
		}
		//添加账户功能
		int num = accountService.editAccount(accountDTO);
		
		return num > 0 ? responseSuccess() : responseFail();
	}
	
	/**
	 * 删除账号
	 * @throws BusinessException 
	 *
	 * @Author：liaozhanggen
	 * @Create Date：2017年2月18日
	 * @param：....
	 * @return：String
	 */
	@ApiOperation(value="删除 （软）账号")
	@GetMapping("/deleteaccount/{id}")
	public String removeAccount(@PathVariable(name="id",required=true) int id) throws BusinessException{
		if (id== 0) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.PARAMETER_ERROR);
		}
		//删除账户功能
		int num = accountService.removeAccount(id);
		return num > 0 ? responseSuccess() : responseFail(CommonConstant.DATA_VALID_DELETED);
	}
	
	
	/**
	 * 账号查询列表分页
	 *
	 * @Author：liaozhanggen
	 * @Create Date：2017年2月18日
	 * @param：....
	 * @return：String
	 */
	@ApiOperation(value="账号查询列表分页")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "roleId", value = "角色Id", dataType = "int", required = false, paramType = "query"),
		@ApiImplicitParam(name = "userAccount", value = "账户号", dataType = "String", required = false, paramType = "query"),
		@ApiImplicitParam(name = "realName", value = "真实名称", dataType = "String", required = false, paramType = "query"),
		@ApiImplicitParam(name = "userCode", value = "操作编号", dataType = "String", required = false, paramType = "query"),
		@ApiImplicitParam(name = "pageNum", value = "起始页", dataType = "int", required = true, paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页行", dataType = "int", required = true, paramType = "query")
	})
	@GetMapping("/queryaccount")
	public String queryAccount(AccountSearchDTO accSearchDTO) throws BusinessException{
		
		//账号查询列表分页
		return responseSuccess(accountService.queryAccountPage(accSearchDTO).toPageInfo());
	}

	/**
	 * 单个账号详情
	 * @throws BusinessException 
	 *
	 * @Author：liaozhanggen
	 * @Create Date：2017年2月18日
	 * @param：....
	 * @return：String
	 */
	@ApiOperation(value="单个账号详情")
	@GetMapping("/detailaccount/{id}")
	public String detailAccount(@PathVariable(name="id",required=true) int id) throws BusinessException{
		if (id== 0) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.PARAMETER_ERROR);
		}
		return responseSuccess(accountService.detailAccount(id));
	}
	
	/**
	 * 根据帐户或编号重置帐户密码
	 * @throws BusinessException 
	 *
	 * @Author：liaozhanggen
	 * @Create Date：2017年2月18日
	 * @param：....
	 * @return：String
	 */
	@ApiOperation(value="根据帐户或编号-重置帐户密码")
	@GetMapping("/resetpwd")
	public String restPwdAccount(@RequestParam(name="userAccount",required=true)String userAccount) throws BusinessException{
		if (null == userAccount || StringUtils.isEmpty(userAccount.trim())) {
			throw new BusinessException(ResultCode.FAILED, CommonConstant.PARAMETER_ERROR);
		}
		AccountVO account = accountService.restAccount(userAccount);
		if (null != account) {
			account.setId(0);
		}
		return null == account ? responseFail(String.format(CommonConstant.COMMON_IS_VALID,userAccount)) : responseSuccess(account);
	}
	
	/**
	 * 根据帐户或编号重置帐户密码
	 * @throws BusinessException 
	 *
	 * @Author：liaozhanggen
	 * @Create Date：2017年2月18日
	 * @param：....
	 * @return：String
	 */
	@ApiOperation(value="登录")
	@PostMapping("/login")
	public String login(@RequestBody AccountDTO accountDTO) throws BusinessException{
		
		AccountVO account = accountService.loginAccount(accountDTO);
		if (null != account) {
			account.setId(0);
		}
		return null == account ? responseFail(CommonConstant.LOGIN_VALID_ERROR) : responseSuccess(account);
	}
	
}
